package tpf.designer.pattern.course4.structure_pattern1.homework2;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EnhanceDbOperator extends AbstractDbOperator {
	
	private DBOperator dbOperator;
	public EnhanceDbOperator(DBOperator dbOperator){
		this.dbOperator = dbOperator;
	}
	
	//定义一个缓存，实际案例中可以采用guava cache,mapdb,redis等组件
	private Map<String, Object> cache = new ConcurrentHashMap<String, Object>();
	
	@Override
	public Object query(Object queryCondition) {

		//检查缓存中是否有缓存,如果有则返回缓存结果
		if(cache.containsKey(queryCondition.toString()))
			return cache.get(queryCondition.toString());
		
		return dbOperator.query(queryCondition);
	}

	@Override
	public int update(Object updateObject) {
		
		//做一些安全检查
		//TODO
		//if(检查不通过)
		//	return 0;
		//else 更新
		return update(updateObject);
	}
	
}